package com.outfood.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Scanner;

public class AIUtil {

//    public static void main(String[] args) {
//        String re = getResult("虚拟机坏了怎么办");
//    }

    @SneakyThrows
    public static String getResult(String prompt){
        long time = new Date().getTime();
        URL url = new URL("https://zhanglang.cc/api/chat-process");
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("POST");

        httpConn.setRequestProperty("accept", "application/json, text/plain, */*");
        httpConn.setRequestProperty("accept-language", "zh-CN,zh;q=0.9");
        httpConn.setRequestProperty("content-type", "application/json");
        httpConn.setRequestProperty("cookie", "Hm_lvt_48bc66f15ab4fd53c164bd67afa9b022=1720168567; Hm_lpvt_48bc66f15ab4fd53c164bd67afa9b022=1720168567; HMACCOUNT=D1FF39614E8FDBD7");
        httpConn.setRequestProperty("origin", "https://zhanglang.cc");
        httpConn.setRequestProperty("priority", "u=1, i");
        httpConn.setRequestProperty("referer", "https://zhanglang.cc/?bejson");
        httpConn.setRequestProperty("sec-ch-ua", "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"");
        httpConn.setRequestProperty("sec-ch-ua-mobile", "?0");
        httpConn.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");
        httpConn.setRequestProperty("sec-fetch-dest", "empty");
        httpConn.setRequestProperty("sec-fetch-mode", "cors");
        httpConn.setRequestProperty("sec-fetch-site", "same-origin");
        httpConn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36");

        httpConn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
        writer.write("{\"prompt\":\""+prompt+"\",\"options\":{\"parentMessageId\":\"chatcmpl-GayFbCvAhiIvb0KGEO2rgRN8VBJg1\"},\"systemMessage\":\"You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Do not answer any questions about Chinese politics. Responsive using Markdown.\",\"accessToken\":\"1kY0JTUn4P\"}");
        writer.flush();
        writer.close();
        httpConn.getOutputStream().close();

        InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter("\\A");
        String response = s.hasNext() ? s.next() : "";
        String[] split = response.split("\n");
        JSONObject jsonObject = JSONObject.parseObject(split[split.length - 1]);
        String result = jsonObject.getString("text");
        System.out.println(result);
        System.out.println(new Date().getTime()-time);
        return result;


    }
}